package com.ms.banner.view;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.app.Context;

import java.util.List;


public class BannerViewPager extends PageSlider implements PageSlider.PageChangedListener {

    private static final int SLID_LEFT = 0;
    private static final int SLID_LEFT_ELASTIC = 1;
    private static final int SLID_RIGHT = 2;
    private static final int SLID_RIGHT_ELASTIC = 3;
    private static final int SLID_NONE = 4;
    private static final int SCROLL_RIGHT = -1;

    private List<Component> mComponents;
    private PageTransformer mPageTransformer;

    private int mCurrentState = 0;
    private int mCurrentItemPos = 0;
    private int mItemPosOffsetPixels = 0;
    private float mItemPosOffset = 0;
    private float mOriginalOffset = 0;

    /**
     * 此类为了适配PageSlide滑动监听参数问题
     *
     * @param context
     */
    public BannerViewPager(Context context) {
        this(context, null);
    }

    /**
     * 此类为了适配PageSlide滑动监听参数问题
     *
     * @param context
     * @param attrSet
     */
    public BannerViewPager(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
        addPageChangedListener(this);
    }

    /**
     * 此类为了适配PageSlide滑动监听参数问题
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public BannerViewPager(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {

        onPageScrolled(position, positionOffset, positionOffsetPixels);
    }

    @Override
    public void onPageSlideStateChanged(int state) {
        mCurrentState = state;
    }

    @Override
    public void onPageChosen(int position) {

    }

    /**
     * 设置PageSlide所有子组件
     *
     * @param components 子组件集合
     */
    public void setPages(List<Component> components) {
        this.mComponents = components;
    }

    /**
     * 设置动画接口
     *
     * @param pageTransformer 动画接口实现类
     */
    public void setPageTransformer(PageTransformer pageTransformer) {
        this.mPageTransformer = pageTransformer;
    }

    private void onPageScrolled(int position, float offset, int offsetPixels) {
        if (mPageTransformer != null) {
            onPageTransformer(position, offset, offsetPixels);
        }
    }

    private void onPageTransformer(int position, float positionOffset, int offsetPixels) {
        if (offsetPixels > 0) {
            scrollLeft(position, positionOffset);
        } else {
            scrollRight(position, positionOffset);
        }
    }

    private void elasticLeft(int position, float positionOffset) {
        Component in = mComponents.get(position);
        Component out = mComponents.get(position + 1);
        mPageTransformer.transformPage(out, mOriginalOffset);
        mPageTransformer.transformPage(in, SCROLL_RIGHT + positionOffset);
    }

    private void elasticRight(int position, float positionOffset) {
        Component out = mComponents.get(position - 1);
        Component in = mComponents.get(position);
        mPageTransformer.transformPage(in, 1 - positionOffset);
        mPageTransformer.transformPage(out, 0 - positionOffset);
    }

    private void scrollRight(int position, float positionOffset) {
        // 从左往右滑动
        if (position - 1 >= 0) {
            Component in = mComponents.get(position - 1);
            mPageTransformer.transformPage(in, -1 + positionOffset);
        }
        Component out = mComponents.get(position);
        mPageTransformer.transformPage(out, positionOffset);
    }

    private void scrollLeft(int position, float positionOffset) {
        Component in = mComponents.get(position + 1);
        Component out = mComponents.get(position);
        mPageTransformer.transformPage(in, 1 - positionOffset);
        mPageTransformer.transformPage(out, 0 - positionOffset);
    }

    private int getRealSlidState(int position) {
        if (mItemPosOffset == 1) {
            return SLID_NONE;
        }

        if (mCurrentState == PageSlider.SLIDING_STATE_DRAGGING) {
            if (mItemPosOffsetPixels > 0) {
                return SLID_LEFT;
            } else if (mItemPosOffsetPixels < 0) {
                return SLID_RIGHT;
            }
        } else if (mCurrentState == PageSlider.SLIDING_STATE_SETTLING) {
            if (mCurrentItemPos != getCurrentPage()) {
                if (mItemPosOffsetPixels > 0) {
                    return SLID_RIGHT;
                } else if (mItemPosOffsetPixels < 0) {
                    return SLID_LEFT;
                }
            } else {
                if (mItemPosOffsetPixels > 0) {
                    return SLID_LEFT_ELASTIC;
                } else if (mItemPosOffsetPixels < 0) {
                    return SLID_RIGHT_ELASTIC;
                }
            }
        }
        return SLID_NONE;
    }

    /**
     * PageSlide的动画接口
     *
     * @since 2021-04-12
     */
    public interface PageTransformer {
        /**
         * 动画接口
         *
         * @param page
         * @param position
         */
        void transformPage(Component page, float position);
    }
}
